#include "RosterPacket.h"




std::string RosterPacket::getChildElementXML() {
	std::string buf("<query xmlns=\"jabber:iq:roster\">");
	//synchronized (rosterItems) {
	for (std::list<Item*>::iterator it = rosterItems.begin()
			it != rosterItems.end(); it ++) {
		buf.append((*it)->toXML());
	}
	//}
	buf.append("</query>");
	return buf;
}


RosterPacket::Item::Item(std::string & user, std::string & name) :
	user(user),
	name(name),
	itemType(undefined)
	itemStatus(NULL)
{
	std::transform(user.begin(), user.end(), user.begin(), ::tolower);
}


std::string RosterPacket::Item::toXML() {
	std::string buf;
	buf.append("<item jid=\"").append(user).append("\"");
	if (!name.empty()) {
		buf.append(" name=\"").append(StringUtils::escapeForXML(name)).append("\"");
	}
	if (itemType != undefined) {
		buf.append(" subscription=\"").append(itemType).append("\"");
	}
	if (itemStatus != NULL) {
		buf.append(" ask=\"").append(itemStatus->toString()).append("\"");
	}
	buf.append(">");
	for (std::set<std::string>::iterator it = groupNames.begin();
			it != groupNames.end(); it ++) {
		buf.append("<group>").append(StringUtils::escapeForXML(*it)).append("</group>");
	}
	buf.append("</item>");
	return buf;
}



ItemStatus * RosterPacket::ItemStatus::SUBSCRIPTION_PENDING = new ItemStatus("subscribe");
ItemStatus * RosterPacket::ItemStatus::UNSUBSCRIPTION_PENDING = new ItemStatus("unsubscribe");




